//
//  MbRewardAd.m
//  MbAdsSdk
//
//  Created by 吴奶强 on 2021/12/17.
//

#import "MbFullScreenAd.h"
#import <BUAdSDK/BUAdSDK.h>
#import <KSAdSDK/KSAdSDK.h>
#import <ABUADSDK/ABUADSDK.h>
#import "GDTUnifiedInterstitialAd.h"


@interface MbFullScreenAd()<BUNativeExpressFullscreenVideoAdDelegate,ABUFullscreenVideoAdDelegate,GDTUnifiedInterstitialAdDelegate,KSFullscreenVideoAdDelegate>

@property(nonatomic,strong)BUNativeExpressFullscreenVideoAd *ttFullVideoAd;
@property(nonatomic,strong)ABUFullscreenVideoAd *gromoreFullVideoAd;
@property(nonatomic,strong)GDTUnifiedInterstitialAd *gdtVideoAd;
@property(nonatomic,strong)KSFullscreenVideoAd *ksVideoAd;

@property(nonatomic,assign)NSInteger index;

@property(nonatomic,strong)NSError *error;

@end

@implementation MbFullScreenAd


-(void)loadFullScreen{
    if(!self.adItems || self.adItems.count == self.index ){
        if(!self.error){
            self.error = [[NSError alloc]initWithDomain:@"配置信息错误" code:0 userInfo:nil];
        }
        if ([self.delegate respondsToSelector:@selector(fullScreenAdDidError:)]) {
            [self.delegate fullScreenAdDidError:self.error];
        }
        return;
    }
    AdItem *adItem = [self.adItems objectAtIndex:self.index];
    if(adItem.plat == Pangolin){
        self.ttFullVideoAd = [[BUNativeExpressFullscreenVideoAd alloc] initWithSlotID:adItem.codeNo];
        self.ttFullVideoAd.delegate = self;
        [self.ttFullVideoAd loadAdData];
        
        
    } else if(adItem.plat == GDT){
        
        self.gdtVideoAd = [[GDTUnifiedInterstitialAd alloc] initWithPlacementId:adItem.codeNo];
        self.gdtVideoAd.delegate = self;
        self.gdtVideoAd.videoMuted = NO; // 设置视频是否Mute
        [self.gdtVideoAd loadFullScreenAd]; // 加载插屏全屏视频广告
        
    } else if(adItem.plat == KS){
        self.ksVideoAd = [[KSFullscreenVideoAd alloc] initWithPosId:adItem.codeNo];
        self.ksVideoAd.delegate = self;
        [self.ksVideoAd loadAdData];
    } else if(adItem.plat == GroMore){
        self.gromoreFullVideoAd = [[ABUFullscreenVideoAd alloc] initWithAdUnitID:adItem.codeNo];
        self.gromoreFullVideoAd.delegate = self;
        self.gromoreFullVideoAd.mutedIfCan = NO;//静音
        
        // 如果需要使用gdt express 2.0，请设置useExpress2IfCanForGDT=YES;，如果开发者在平台配置了对应代码位的该属性则无需设置；否则开发者需要设置useExpress2IfCanForGDT属性来告知SDK当前广告位下配置的是否为模板2.0；平台配置优先于useExpress2IfCanForGDT设置；在SDK V2900以上配合GdtV4.12.80+全屏视频客户端将无需区分模板2.0
    //    self.fullscreenVideoAd.useExpress2IfCanForGDT = YES;

        //该逻辑用于判断配置是否拉取成功。如果拉取成功，可直接加载广告，否则需要调用setConfigSuccessCallback，传入block并在block中调用加载广告。SDK内部会在配置拉取成功后调用传入的block
        __weak typeof(self) weakself = self;
        //当前配置拉取成功，直接loadAdData
        if([ABUAdSDKManager configDidLoad]) {
            [self.gromoreFullVideoAd loadAdData];
        }else{
            //当前配置未拉取成功，在成功之后会调用该callback
            [ABUAdSDKManager addConfigLoadSuccessObserver:self withAction:^(id  _Nonnull observer) {
                [weakself.gromoreFullVideoAd loadAdData];
            }];
        }
    }
    self.index ++;
}

- (void)showFullScreen:(UIViewController *)rootVC{
    if (self.gromoreFullVideoAd && self.gromoreFullVideoAd.isReady){
        [self.gromoreFullVideoAd showAdFromRootViewController:rootVC];
    }
    if (self.ttFullVideoAd){
        [self.ttFullVideoAd showAdFromRootViewController:rootVC];
    }
    if (self.gdtVideoAd && [self.gdtVideoAd isAdValid]){
        [self.gdtVideoAd presentFullScreenAdFromRootViewController:rootVC];
    }

    if (self.ksVideoAd){
        [self.ksVideoAd showAdFromRootViewController:rootVC];
    }
    
}
#pragma mark 穿山甲回调
- (void)nativeExpressFullscreenVideoAdDidLoad:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidLoad)]) {
        [self.delegate fullScreenAdDidLoad];
    }
}

- (void)nativeExpressFullscreenVideoAdDidDownLoadVideo:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidDownLoadVideo)]) {
        [self.delegate fullScreenAdDidDownLoadVideo];
    }
}

- (void)nativeExpressFullscreenVideoAdDidVisible:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd{
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidVisible)]) {
        [self.delegate fullScreenAdDidVisible];
    }
}

- (void)nativeExpressFullscreenVideoAd:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *)error{
    self.error = error;
    [self loadFullScreen];
}


#pragma mark 优量汇回调
- (void)unifiedInterstitialSuccessToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidLoad)]) {
        [self.delegate fullScreenAdDidLoad];
    }
}

- (void)unifiedInterstitialRenderSuccess:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidDownLoadVideo)]) {
        [self.delegate fullScreenAdDidDownLoadVideo];
    }
}

- (void)unifiedInterstitialWillPresentScreen:(GDTUnifiedInterstitialAd *)unifiedInterstitial{
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidVisible)]) {
        [self.delegate fullScreenAdDidVisible];
    }
}


- (void)unifiedInterstitialFailToLoadAd:(GDTUnifiedInterstitialAd *)unifiedInterstitial error:(NSError *)error{
    self.error = error;
    [self loadFullScreen];
}

#pragma mark 快手回调
- (void)fullscreenVideoAdVideoDidLoad:(KSFullscreenVideoAd *)fullscreenVideoAd{
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidDownLoadVideo)]) {
        [self.delegate fullScreenAdDidDownLoadVideo];
    }
}




#pragma mark gromore回调
- (void)fullscreenVideoAdDidLoad:(NSObject *)fullscreenVideoAd{
    //ABUFullscreenVideoAd,KSFullscreenVideoAd
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidLoad)]) {
        [self.delegate fullScreenAdDidLoad];
    }
}

- (void)fullscreenVideoAdDidDownLoadVideo:(ABUFullscreenVideoAd *)fullscreenVideoAd{
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidDownLoadVideo)]) {
        [self.delegate fullScreenAdDidDownLoadVideo];
    }
}

- (void)fullscreenVideoAdDidVisible:(NSObject *)fullscreenVideoAd{
    //ABUFullscreenVideoAd,KSFullscreenVideoAd
    if ([self.delegate respondsToSelector:@selector(fullScreenAdDidVisible)]) {
        [self.delegate fullScreenAdDidVisible];
    }
}

- (void)fullscreenVideoAd:(NSObject *)fullscreenVideoAd didFailWithError:(NSError *)error{
    //KSFullscreenVideoAd,ABUFullscreenVideoAd
    self.error = error;
    [self loadFullScreen];
}




@end
